<template>
    <div>
        <el-card>
            <h1 slot="header">机审设置</h1>
            <el-form
                ref="queryForm"
                label-suffix="："
                :model="queryForm"
                label-position="right"
                label-width="135px"
            >
                <el-form-item
                    label="毕业年份"
                    prop="grade"
                    :rules="[{ required: true, message: '毕业年份必须填写', trigger: ['blur', 'change'] }]"
                >
                    <el-date-picker
                        :placeholder="$t('common.pla.pleaseSelect')"
                        v-model="queryForm.grade"
                        value-format="yyyy"
                        align="right"
                        type="year"
                        clearable
                        style="width: 180px"
                    />
                    <el-button type="primary"  @click="setNumeber('name')" style="margin-left: 5px;">
                            {{ $t('graduationList.btn5') }}
                    </el-button>
                </el-form-item>
                <el-form-item
                    label="审核范围"
                    prop="type"
                    :rules="[{ required: true, message: '审核范围必须填写', trigger: ['blur', 'change'] }]"
                >
                    <el-select
                        :placeholder="$t('common.pla.pleaseSelect')"
                        v-model="queryForm.type"
                        style="width: 180px;"
                        clearable
                        filterable
                    >
                        <el-option
                            v-for="item in options.typeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                    <span style="color:red;font-size:12px">
                        {{
                            queryForm.type === '1'
                                ? '当前毕业审核默认最后一个学期成绩通过'
                                : queryForm.type === '0'
                                ? '当前毕业审核全部课程'
                                : ''
                        }}
                    </span>
                </el-form-item>
                <el-form-item>
                    <el-button @click="resetForm('queryForm')">重置</el-button>
                    <el-button type="primary" @click="submitForm('queryForm')">
                        提交
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import { setMachineType, findMachineType, createGraduateList} from 'common/src/api/graduationManagement/graduationList'
export default {
    name: 'machineAuditSetting',
    data() {
        return {
            queryForm: {
                grade: '',
                type: '',
                isOverseas: '0'
            },
            options: {
                typeList: [
                    {
                        value: '0',
                        label: '全审'
                    },
                    {
                        value: '1',
                        label: '部审'
                    }
                ]
            }
        }
    },
    mounted() {
        this.init()
    },
    methods: {
        async init() {
            let grade = new Date().getFullYear()
            let { code, data } = await findMachineType({ grade: grade, isOverseas: '0' })
            if (code !== 200 && !data) return
            this.queryForm.grade = data.grade
            this.queryForm.type = data.type
            this.saveInfo = {
                grade: data.grade,
                type: data.type
            }   
        },
        async request(api) {
            let { code, data } = await api
            if (code !== 200) return
            this.$message({
                type: 'success',
                message: '设置机审成功！'
            })
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.request(setMachineType(this.queryForm))
                } else {
                    return false
                }
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            if (this.saveInfo) {
               Object.assign(this.queryForm, this.saveInfo);
            }
        },
        setNumeber() {
            if (!this.queryForm.grade) {
                this.$message({
                    type: 'warning',
                    message: '请选择毕业年份'
                })
                return;
            }
            let param = {
                graduationDate: this.queryForm.grade,
                isOverseas: "0"
            }
            createGraduateList(param).then(response => {
                if  (response.code === 200) {
                    this.$message({
                        type: "success",
                        message: "初始化成功"
                    }); 
                }
            });
        }
    }
}
</script>

<style>
</style>